package com.gemme.gemme.view;

import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gemme.gemme.R;
import com.gemme.gemme.util.StrUtil;
import com.gemme.gemme.wode.DingDanBean;

import java.text.DecimalFormat;
import java.util.List;

public class SingleOrderView extends RelativeLayout {

    private Context context;
    private DecimalFormat mFormat = new DecimalFormat("0.00");

    public SingleOrderView(Context context) {
        this(context,null);
    }

    public SingleOrderView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SingleOrderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
    ImageView image;
    TextView name;
    TextView guige;
    TextView order_price;
    TextView tv_original_price;
    TextView tv_acount;
    TextView tv_brand_name;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.single_order_view,this);
//        textview.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG); //中划线
        tv_original_price = findViewById(R.id.tv_original_price);
        tv_original_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG); //中划线
        image = findViewById(R.id.image);
        name = findViewById(R.id.name);
        guige = findViewById(R.id.guige);
        order_price = findViewById(R.id.order_price);
        tv_acount = findViewById(R.id.tv_acount); //数量
        tv_brand_name = findViewById(R.id.tv_brand_name);
    }

    public void setData(DingDanBean.DataBean.ShopProductVoListBean bean) {
        try {
            Glide.with(context).load(bean.getAttributeSpecImage()).into(image);
            name.setText(StrUtil.noNUll(bean.getProductName()));
            guige.setText(StrUtil.noNUll(getGuige(bean)));
            double discountPrice = bean.getDiscountPrice();

            order_price.setText("¥"+ mFormat.format((discountPrice <= 0 ? bean.getPrice() : discountPrice)));
            tv_original_price.setText("¥"+ mFormat.format(bean.getPrice()));
            tv_acount.setText(bean.getPurchasesNum() + "");
            tv_brand_name.setText(bean.getBrandName() + "");
        }catch (Exception e){

        }
    }

    private String getGuige(DingDanBean.DataBean.ShopProductVoListBean bean) {
        String str = "";
        try {
            List<DingDanBean.DataBean.ShopProductVoListBean.AttributesBean> attributes = bean.getAttributes();
            for (int i = 0; i < attributes.size(); i++) {
                DingDanBean.DataBean.ShopProductVoListBean.AttributesBean attributesBean = attributes.get(i);
                str = str + attributesBean.getAttributeName() + attributesBean.getAttributeSpecName();
            }
        }catch (Exception e){

        }
        return str;
    }
}
